package com.talkweb.newproject.admin.sysrole.service;

import com.talkweb.newproject.admin.sysorg.dao.SysOrgDao;
import com.talkweb.newproject.admin.sysrole.dao.SysRoleDao;
import com.talkweb.newproject.admin.sysrole.dto.SysRoleDTO;
import com.talkweb.newproject.admin.sysrolemapping.dto.SysRoleMappingDTO;
import com.talkweb.newproject.api.model.admin.SysRoleParam;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.security.krb5.internal.PAData;

import javax.xml.crypto.dsig.spec.ExcC14NParameterSpec;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qsf on 2019/4/18.
 */
@Service
public class SysRoleService {
    @Autowired
    private SysRoleDao sysRoleDao;

    /**
     * 新增角色
     */
    @Transactional(rollbackFor = {Exception.class})
    public Long insertSysRole(SysRoleDTO sysRoleDTO) throws Exception {
        Map<String,Object> paramsMap = new HashMap();
        paramsMap.put("dto",sysRoleDTO);
        int count = sysRoleDao.insertSysRole(paramsMap);
        SysRoleDTO result = (SysRoleDTO) paramsMap.get("dto");
        return result.getId();
    }

    /**
     * 修改角色信息
     */
    @Transactional(rollbackFor = {Exception.class})
    public void updateSysRole(SysRoleDTO sysRoleDTO) throws Exception{
        Map<String,Object> params = new HashMap();
        params.put("dto",sysRoleDTO);
        sysRoleDao.updateSysRole(params);
    }

    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteSysRole(String id) throws Exception{
        int count = sysRoleDao.deleteSysRoleById(id);
        if(count>0) {
            return true;
        }else {
            return false;
        }
    }

    /**
     * 根据id查询角色信息
     */
    public SysRoleDTO querySysRoleByPrimaryKey(String id) throws Exception {
        SysRoleDTO result = sysRoleDao.querySysRoleByPrimaryKey(id);
        if(result==null) {
            result = new SysRoleDTO();
        }
        return result;
    }

    @Transactional(rollbackFor = {Exception.class})
    public List<SysRoleDTO> selectSysRoleByUserId(SysRoleDTO sysRoleDTO) throws Exception {
        Map<String ,Object> params = new HashMap();
        params.put("dto",sysRoleDTO);
        return sysRoleDao.selectSysRoleByUserId(params);
    }

    public Object searchSysRole(SysRoleParam dto) throws Exception{
        Map<String,Object> params = new HashMap();
        params.put("dto",dto);
        List<SysRoleDTO> sysRoleDTOList = sysRoleDao.searchSysRole(params);
        return sysRoleDTOList;
    }

    public SysRoleDTO queryLikeSysRole(SysRoleDTO obj) throws Exception{
        Map<String,Object> params = new HashMap();
        params.put("dto",obj);
        return sysRoleDao.queryLikeSysRole(params);
    }
}
